﻿Public Class frmParejas
    Dim cont As Integer = 0
    Dim i As Integer
    Dim WithEvents juego As JugarParejas
    Dim picAnterior As New PictureBox
    Dim picActivo As New PictureBox
    Dim tick As Integer = 50
    Dim puntos As Integer = 0
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        juego = New JugarParejas
        TimerCronometro.Start()
    End Sub

    Private Sub pbx1_Click(sender As System.Object, e As System.EventArgs) Handles pbx1.Click, pbx2.Click, pbx3.Click, pbx4.Click, pbx5.Click, pbx6.Click, pbx7.Click, pbx8.Click, pbx9.Click, pbx10.Click, pbx11.Click, pbx12.Click, pbx13.Click, pbx14.Click, pbx15.Click, pbx16.Click, pbx17.Click, pbx18.Click, pbx19.Click, pbx20.Click, pbx21.Click, pbx22.Click, pbx23.Click, pbx24.Click
        picActivo = DirectCast(sender, PictureBox)
        i = picActivo.Tag
        Dim numImagen As Integer = juego.nombrar(picActivo)
        juego.destapar(picActivo, numImagen)
        If juego.NumClick = 0 Then picAnterior = picActivo
        picActivo.Enabled = False
        juego.sumarClick()
        If haTerminado() Then
            puntos = puntos * 2
            lblPuntos.Text = puntos
            TimerCronometro.Stop()
            MiJuego.UsuarioActual.SumarPuntos(2, puntos)
            MessageBox.Show("¡¡Enhorabuena!! Has conseguido " & puntos & "Puntos.")
        End If
    End Sub

    Private Sub juego_DosClick(num As Integer) Handles juego.DosClick
        If juego.encontrado(picActivo, picAnterior) Then
            picActivo.Enabled = False
            puntos = puntos + 5
            lblPuntos.Text = puntos
        Else
            For Each p As Control In Me.Controls
                If p.GetType = GetType(PictureBox) Then
                    If IsNothing(TryCast(p, PictureBox).Image) Then
                        p.Enabled = False
                    End If
                End If
            Next
            System.Threading.Thread.Sleep(500)
            juego.tapar(picActivo, picAnterior)
            For Each p As Control In Me.Controls
                If p.GetType = GetType(PictureBox) Then
                    If IsNothing(TryCast(p, PictureBox).Image) Then
                        p.Enabled = True
                    End If
                End If
            Next
        End If
        juego.restablecerClick()
    End Sub

    Private Sub TimerCronometro_Tick(sender As Object, e As System.EventArgs) Handles TimerCronometro.Tick
        tick = tick - 1
        lblTiempo.Text = tick
        If tick = 0 Then
            TimerCronometro.Stop()
            For Each pic As Control In Me.Controls
                If pic.GetType() = GetType(PictureBox) Then
                    If IsNothing(TryCast(pic, PictureBox).Image) Then
                        pic.Enabled=False
                    End If
                End If
            Next
            MiJuego.UsuarioActual.SumarPuntos(2, puntos)
            MessageBox.Show("¡Se acabó el tiempo! Has conseguido " & puntos & " Puntos.")
        End If
    End Sub
    Private Function haTerminado() As Boolean
        For Each pic As Control In Me.Controls
            If pic.GetType() = GetType(PictureBox) Then
                If IsNothing(TryCast(pic, PictureBox).Image) Then Return False
            End If
        Next
        Return True
    End Function
End Class
